package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemContactsBinding;
import com.swgk.core.base.model.entity.ContactsEntitiy;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.ArrayList;

public class ContanctsAdpater extends BaseRecycleAdapter<ContactsEntitiy> {
    public ContanctsAdpater(Context context) {
        super(context, R.layout.item_contacts, new ArrayList<>());
    }

    @Override
    public void convert(ItemViewHolder holder, ContactsEntitiy contactsEntitiy) {
        if (holder.getmBinding() instanceof ItemContactsBinding) {
            holder.getmBinding().setVariable(BR.model, contactsEntitiy);
            String path = "";
            if (!TextUtils.isEmpty(contactsEntitiy.getProvinceName())) {
                path = path + contactsEntitiy.getProvinceName();
            }
            if (!TextUtils.isEmpty(contactsEntitiy.getCityName())) {
                path = path + contactsEntitiy.getCityName();
            }
            if (!TextUtils.isEmpty(contactsEntitiy.getAreaName())) {
                path = path + contactsEntitiy.getAreaName();
            }
            if (!TextUtils.isEmpty(contactsEntitiy.getAddress())) {
                path = path + contactsEntitiy.getAddress();
            }
            if (!TextUtils.isEmpty(path)) {
                SpannableStringBuilder span = new SpannableStringBuilder(path);
                span.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), 0, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                ((ItemContactsBinding) holder.getmBinding()).tvPath.setText("0".equals(contactsEntitiy.getStatus()) ? span : path);
            } else {
                ((ItemContactsBinding) holder.getmBinding()).tvPath.setText("/");
            }

            if (onItemClick != null) {
                ((ItemContactsBinding) holder.getmBinding()).tvEdit.setOnClickListener(view -> onItemClick.onItemClick(contactsEntitiy, -1));
                ((ItemContactsBinding) holder.getmBinding()).tvDelete.setOnClickListener(view -> onItemClick.onItemClick(contactsEntitiy, -2));
                ((ItemContactsBinding) holder.getmBinding()).imageCheced.setOnClickListener(view -> onItemClick.onItemClick(contactsEntitiy, -3));
                ((ItemContactsBinding) holder.getmBinding()).tvSetstats.setOnClickListener(view -> onItemClick.onItemClick(contactsEntitiy, -3));
            }
            if (!TextUtils.isEmpty(contactsEntitiy.getContactName())) {
                ((ItemContactsBinding) holder.getmBinding()).tvNameTtle.setText(contactsEntitiy.getContactName().substring(0, 1));
            }
            ((ItemContactsBinding) holder.getmBinding()).imageCheced.setImageResource("0".equals(contactsEntitiy.getStatus()) ? R.mipmap.btn_checed : R.mipmap.btn_uncheced);
        }
    }
}
